# -*- coding: utf-8 -*-
'''
Created on 14.11.2019

@author: yu03
'''

from PyUeye_Unified.Hor_16line import file_name, hor_index
from PyUeye_Unified.IDS_lib.pyueye_camera import Camera
from PyUeye_Unified.IDS_lib.pyueye_utils import FrameThread
from PyUeye_Unified.IDS_lib.pyueye_gui import PyuEyeQtApp, PyuEyeQtView
from PyQt5 import QtGui
from pyueye import ueye
import cv2
import numpy as np
import time
from PyQt5.QtWidgets import QApplication
import datetime
from RigolScope_DS4054 import RigolScope

now = datetime.datetime.now()
num = 0
f = open(file_name,'ab')

'''
    Set the Oscilloscope trigger in single mode
'''

# scope = RigolScope.RigolScope('USB0::0x1AB1::0x04B1::DS4A140800046::INSTR')
# print(scope.Read_ID())
# scope.Write_Single()
# print('Single Mode')
'''
    Saving Data to .npy file
'''
def process_image(self, image_data):
    # reshape the image data as 1dimensional array
    time_stamp = time.time()
    global num, f
    num += 1
    '''
        Defination of Frame Number
    '''
    if num == 5000:
        QApplication.quit()
    print(num, time_stamp)
    image = image_data.as_1d_image()
#     print(np.shape(image))
    line = np.array([image[:], time_stamp])
    
    np.save(f, line, allow_pickle=True)
    
    # show the image with Qt
    return QtGui.QImage(image.data,
                        image_data.mem_info.width,
                        image_data.mem_info.height,
                        QtGui.QImage.Format_Grayscale8)
 
def main():
    
    '''
        Parameters Define
    '''
    AOI = [0, hor_index, 1280, 16]
    Exposure = 0.8
    Trigger_External = 0
    Blacklevel = 0, 200
    Data_fomate = 'SENSOR_RAW10'
#     Data_fomate = 'MONO8'
    
    
    # we need a QApplication, that runs our QT Gui Framework    
    app = PyuEyeQtApp()
  
    # a basic qt window
    view = PyuEyeQtView()
#     view.show()
    view.user_callback = process_image
 
    # camera class to simplify uEye API access
    cam = Camera()
    cam.init()
    
    if Data_fomate == 'MONO8':
        cam.set_colormode(ueye.IS_CM_MONO8)
    elif Data_fomate == 'SENSOR_RAW10':
        cam.set_colormode(ueye.IS_CM_SENSOR_RAW10)
    
    if cam.get_colormode() == 33:
        print('Color Mode: Sensor RAW 10')
    elif cam.get_colormode() == 6:
        print('Color Mode: MONO 8')
#     print(ueye.IS_CM_BGR8_PACKED)

    if Blacklevel[0] == 0:
        cam.set_blacklevel_mode(ueye.IS_AUTO_BLACKLEVEL_OFF)
        cam.set_blacklevel(Blacklevel[1])
        print('Blacklevel Mode: Manual', cam.get_blacklevel_mode(), 'Blacklevel:', cam.get_blacklevel())

    cam.set_aoi(AOI[0], AOI[1], AOI[2], AOI[3])
    aoi = cam.get_aoi()
    print('AOI:', aoi.x, aoi.y, aoi.width, aoi.height)
    
    if Trigger_External == 1:
        cam.set_external_trigger()
    elif Trigger_External == 0:
        print('Framerate Range:', cam.get_FrameTimeRange()[0], cam.get_FrameTimeRange()[1],cam.get_FrameTimeRange()[2])
        cam.set_fps(1000)
#         cam.set_fps(1/cam.get_FrameTimeRange()[0])

    cam.set_exposure(Exposure)
    print('Exposure Time:', cam.get_exposure())
    cam.turn_on_flash_high()
    cam.set_flash_params(-30,500)
#     print(cam.get_colordepth()[0], cam.get_colordepth()[1])
    cam.alloc()
    cam.capture_video()
#     cam.freeze_video()
     
    #a thread that waits for new images and processes all connected views
    thread = FrameThread(cam, view)
#     thread.setDaemon(True)
    thread.start()
    
    # cleanup
    app.exit_connect(thread.stop)
    app.exec_()
  
    print('Frame Rate:', cam.get_fps())
    thread.stop()
    thread.join()
 
    cam.stop_video()
    cam.exit()
 
if __name__ == "__main__":
    main()